const mongoose = require('mongoose');

// 技师模型继承自用户模型，添加技师特有字段
const technicianSchema = new mongoose.Schema({
    // 继承用户基础字段
    username: {
        type: String,
        required: true,
        trim: true
    },
    password: {
        type: String,
        required: true
    },
    email: {
        type: String,
        trim: true
    },
    phone: {
        type: String,
        trim: true
    },
    sex: {
        type: String,
        enum: ['男', '女'],
        default: '男'
    },
    age: {
        type: Number,
        min: 0
    },
    role: {
        type: mongoose.Schema.Types.ObjectId,
        ref: 'Role',
        required: true
    },
    status: {
        type: Boolean,
        default: true
    },
    createTime: {
        type: Date,
        default: Date.now
    },

    // 技师特有字段
    avatar: {
        type: String,
        default: ''
    },
    rating: {
        type: Number,
        default: 5.0,
        min: 0,
        max: 5.0
    },
    totalRatings: {
        type: Number,
        default: 0,
        comment: '总评分次数'
    },
    workStatus: {
        type: String,
        enum: ['空闲中', '服务中', '休息中', '请假中'],
        default: '空闲中'
    },
    skills: [{
        type: String,
        enum: ['面部护理', '身体按摩', '足疗', 'SPA', '美甲', '美发']
    }],
    experience: {
        type: Number,
        default: 0,
        comment: '工作经验（年）'
    },
    workSchedule: {
        startTime: {
            type: String,
            default: '09:00'
        },
        endTime: {
            type: String,
            default: '18:00'
        },
        workDays: [{
            type: Number,
            enum: [0, 1, 2, 3, 4, 5, 6],
            default: [1, 2, 3, 4, 5, 6]
        }]
    },
    updateTime: {
        type: Date,
        default: Date.now
    }
});

// 更新时间中间件
technicianSchema.pre('save', function (next) {
    this.updateTime = new Date();
    next();
});

// 静态方法：根据角色ID获取技师列表
technicianSchema.statics.findByRole = function (roleId) {
    return this.find({ role: roleId, status: true });
};

// 静态方法：根据技能获取技师列表
technicianSchema.statics.findBySkills = function (skills) {
    return this.find({
        role: '689dc4e6889e4412b3bdfed2',
        status: true,
        skills: { $in: skills }
    });
};

module.exports = mongoose.model('Technician', technicianSchema); 